﻿package net.wangit.view.enumeration;

import java.util.HashMap;
import java.util.Map;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.util.ArrayUtil;


public class EnumerationUtil {

	private static final MapList BLANK_ENUMERATION;
	private static final Map<String, String> BLANK_MAP;

	public EnumerationUtil() {
	}

	public static MapList getBlankEnumeration() {
		return BLANK_ENUMERATION;
	}

	public static Map<String, String> getBlankRow() {
		return BLANK_MAP;
	}

	public static String getEnumerationName(MapList enumeration, String value) {
		if (enumeration == null || value == null)
			return null;
		for (int i = 0; i < enumeration.size(); i++) {
			Row item = enumeration.getRow(i);
			String itemValue = item.get("value");
			if (value.equals(itemValue))
				return item.get("name");
		}

		return null;
	}

	public static String getEnumerationNames(MapList enumeration,
			String values[]) {
		if (values == null)
			return null;
		StringBuffer names = new StringBuffer();
		for (int i = 0; i < enumeration.size(); i++) {
			Row item = enumeration.getRow(i);
			String itemValue = item.get("value");
			if (ArrayUtil.find(values, itemValue) >= 0)
				names.append(item.get("name")).append(",");
		}

		if (names.length() > 0)
			return names.substring(0, names.length() - 1);
		else
			return null;
	}

	static {
		BLANK_ENUMERATION = new MapList();
		BLANK_MAP = new HashMap<String, String>();
		BLANK_ENUMERATION.put(0, "name", "");
		BLANK_ENUMERATION.put(0, "value", "");
		BLANK_MAP.put("name", "");
		BLANK_MAP.put("value", "");
	}
}
